import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: () => import('@/views/login'), name: 'login' },
  {
    path: '/',
    component: () => import('@/views/home'),
    redirect: '/dashboard',
    name: 'home',
    children: [
      // 数据概览
      {
        path: '/dashboard',
        component: () => import('@/views/dashboard'),
        name: ''
      },
      // 用户
      {
        path: '/base/users',
        component: () => import('@/views/users'),
        name: '后台管理 / 用户'
      },
      // 菜单
      {
        path: '/base/menus',
        component: () => import('@/views/menus'),
        name: '后台管理 / 菜单'
      },
      // 权限
      {
        path: '/base/permissions',
        component: () => import('@/views/permissions'),
        name: '后台管理 / 权限'
      },
      // 日志
      {
        path: '/base/logs',
        component: () => import('@/views/logs'),
        name: '后台管理 / 日志'
      },
      // 企业管理
      {
        path: '/companys/list',
        component: () => import('@/views/companys'),
        name: '企业管理 / 企业管理'
      },
      // 基础题库
      {
        path: '/questions/list',
        component: () => import('@/views/questions-list'),
        name: '题库管理 / 基础题库'
      },
      // 精选题库
      {
        path: '/questions/choice',
        component: () => import('@/views/questions-choice'),
        name: '题库管理 / 精选题库'
      },
      // 试题录入
      {
        path: '/questions/new',
        component: () => import('@/views/questions-new'),
        name: '题库管理 / 试题录入'
      },
      // 组题管理
      {
        path: '/questions/randoms',
        component: () => import('@/views/questions-randoms'),
        name: '题库管理 / 组题管理'
      },
      // 学科
      {
        path: '/subjects/list',
        component: () => import('@/views/subjects-list'),
        name: '学科管理 / 学科'
      },
      // 目录
      {
        path: '/subjects/directorys',
        component: () => import('@/views/subjects-directorys'),
        name: '学科管理 / 目录'
      },
      // 标签
      {
        path: '/subjects/tags',
        component: () => import('@/views/subjects-tags'),
        name: '学科管理 / 标签'
      },
      // 面试技巧
      {
        path: '/articles/list',
        component: () => import('@/views/articles'),
        name: '面试技巧 / 面试技巧'
      }
    ]
  }
]
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to是将要访问的路径
  if (to.path === '/login') return next()
  const tokenStr = store.state.user
  if (!tokenStr) return next('/login')
  next()
})
export default router
